package com.itdaan.portal.business.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.itdaan.portal.business.controller.ShiroDbRealm.ShiroUser;
import com.itdaan.portal.business.domain.Collections;
import com.itdaan.portal.business.domain.Page;
import com.itdaan.portal.business.domain.ReturnDTO;
import com.itdaan.portal.business.service.CollectionService;
import com.itdaan.portal.common.exception.ServiceException;
import com.itdaan.portal.common.exception.ViewException;

/**
 * 收藏业务控制类
 * @author www.itdaan.com
 */
@Controller
@RequestMapping(value = "/user/collection")
@SessionAttributes
public class CollectionController {

	private Logger logger = LoggerFactory.getLogger(CollectionController.class);
	
	@Resource(name = "collectionService")
	private CollectionService collectionService;
	
	/**
	 * 添加收藏记录
	 */
	@RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String,Object> update(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "title", required = true) String title,//标题
            @RequestParam(value = "postId", required = true) Integer postId,//文章ID
            @RequestParam(value = "url", required = true) String url,//文章地址
            @RequestParam(value = "collectionGroupName", required = false) String collectionGroupName,//收藏夹名称
            @RequestParam(value = "collectionGroupId", required = true) Integer collectionGroupId//收藏夹ID
    		) throws ViewException {
		Map<String, Object> modelAndView = new HashMap<String, Object>();
        try{
        	Collections collection = new Collections();
        	collection.setTitle(title);
        	collection.setPostId(postId);
        	collection.setUrl(url);
    		Subject currentUser = SecurityUtils.getSubject();
    		ShiroUser shiroUser = (ShiroUser)currentUser.getPrincipals().getPrimaryPrincipal();
    		collection.setUname(shiroUser.getName());
    		collection.setUserId(shiroUser.getId());
        	ReturnDTO returnDTO = collectionService.insertCollection(collection,collectionGroupName,collectionGroupId);
        	modelAndView.put("success", returnDTO.isSuccess());
        	modelAndView.put("msg", returnDTO.getMsg());
        }catch(Exception e){
			logger.error("后台业务逻辑出现异常", e);
			modelAndView.put("success", false);
			modelAndView.put("msg", "网站出现异常等待程序员GG修复。。。");
    	}
		return modelAndView;
	}
	
	/**
	 * 查询收藏记录
	 */
	@RequiresPermissions("collection:list")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(
			@RequestParam(value = "title",required = false) String title,
			@RequestParam(value = "groupId",required = false) Integer groupId,
			@RequestParam(value = "page", defaultValue = "1") int current,
			Model model) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(title)){
			conditions.put("title", title);
		}
		if(groupId != null){
			conditions.put("groupId", groupId);
		}
		Subject currentUser = SecurityUtils.getSubject();
		ShiroUser shiroUser = (ShiroUser)currentUser.getPrincipals().getPrimaryPrincipal();
		conditions.put("userId", shiroUser.getId());
		Page page = new Page();
		page.setConditions(conditions);
		page.setCurrent(current);
		try {
			collectionService.getByPage(page);
		} catch (ServiceException e) {
			logger.error("查询用户信息失败：{}",e.toString());
		}
		model.addAttribute("page", page);
		return "user/collection_list";		
	}
}
